package com.joe.simpleimg.View

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.graphics.*
import android.util.Log
import com.joe.simpleimg.R


class SimpleImgView(context: Context, attributeSet: AttributeSet?) : View(context, attributeSet) {

    constructor(context: Context) : this(context, null)

    init {

        if (attributeSet != null) {
            parseArrtibute(attributeSet)
        }

    }

    private var paint = Paint()
    private lateinit var drawable: Drawable
    private var imgtype = 0
    private var mradius = 0f


    private fun parseArrtibute(attributeSet: AttributeSet) {
        val typeArray = context.obtainStyledAttributes(attributeSet, com.joe.simpleimg.R.styleable.SimpleImgView)
        drawable = typeArray.getDrawable(R.styleable.SimpleImgView_bitmap)
        imgtype = typeArray.getInt(R.styleable.SimpleImgView_type,0)
        mradius = typeArray.getDimension(R.styleable.SimpleImgView_radius,60f)
        typeArray.recycle()
    }

    override fun onDraw(canvas: Canvas?) {
        if (drawable == null)
            return
        var bitmap = drawableToBitmap(drawable)
        var min = Math.min(bitmap.getWidth(), bitmap.getHeight())
        if(imgtype==0){
            canvas!!.drawBitmap(drawableToCircleBitmap(bitmap, min), 0f, 0f, paint)
        }else if (imgtype==1){
            canvas!!.drawBitmap(drawableToRectBitmap(bitmap, min,mradius), 0f, 0f, paint)
        }else{
            canvas!!.drawBitmap(bitmap, 0f, 0f, paint)
        }


    }


    //drawable转bitmap
    private fun drawableToBitmap(drawable: Drawable): Bitmap {
        var config =
            if (drawable.opacity != PixelFormat.OPAQUE) Bitmap.Config.ARGB_8888 else Bitmap.Config.RGB_565// 取drawable的颜色格式
        var bitmap = Bitmap.createBitmap(width, height, config)// 建立对应bitmap
        var canvas = Canvas(bitmap)// 建立对应bitmap的画布
        drawable.setBounds(0, 0, width, height)
        drawable.draw(canvas)// 把drawable内容画到画布中
        return bitmap
    }

    //剪切为圆形图
    private fun drawableToCircleBitmap(resource: Bitmap, min: Int): Bitmap {
        val paint = Paint()
        paint.isAntiAlias = true
        var config = Bitmap.Config.ARGB_8888// 取drawable的颜色格式
        var bitmap = Bitmap.createBitmap(min, min, config)
        var canvas = Canvas(bitmap)
        canvas.drawCircle((min / 2).toFloat(), (min / 2).toFloat(), (min/2).toFloat(), paint)
        paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
        canvas.drawBitmap(resource, 0f, 0f, paint)
        return bitmap
    }

    //剪切为圆角
    private fun drawableToRectBitmap(resource: Bitmap,min: Int,mRadius: Float):Bitmap{
        val paint = Paint()
        paint.isAntiAlias = true
        var config = Bitmap.Config.ARGB_8888// 取drawable的颜色格式
        var bitmap = Bitmap.createBitmap(min, min, config)
        var canvas = Canvas(bitmap)
        var rectf = RectF(0f,0f, min.toFloat(), min.toFloat())
        canvas.drawRoundRect(rectf, mRadius, mRadius,paint)
        paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
        canvas.drawBitmap(resource, 0f, 0f, paint)
        return bitmap
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //如果控件宽高不一致，取最小值
        val width = Math.min(measuredWidth, measuredHeight)
        setMeasuredDimension(width,width)
    }
}